﻿using System;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webapp4U
{
    public static class ResponseHelper 
    { 
        public static void Redirect(this HttpResponse response, string url, string target, string windowFeatures) 
        { 
            if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures)) 
            { 
                response.Redirect(url); 
            }
            else 
            { 
                Page page = (Page)HttpContext.Current.Handler;
                if (page == null) 
                { 
                    throw new InvalidOperationException("Cannot redirect to new window outside Page context."); 
                } 
                url = page.ResolveClientUrl(url); 
                string script; 
                if (!String.IsNullOrEmpty(windowFeatures)) 
                {
                    script = @"window.open(""{0}"", ""{1}"", ""{2}"");"; 
                } 
                else 
                { 
                    script = @"window.open(""{0}"", ""{1}"");"; 
                } 
                script = String.Format(script, url, target, windowFeatures); 
                ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true); 
            } 
        } 
    }

   public static class Globals
   {
      //public static string CurrentUserName
      // {
      //     get
      //     {
      //         string userName = "";
      //         if (HttpContext.Current.User.Identity.IsAuthenticated)
      //             userName = HttpContext.Current.User.Identity.Name;
      //         return userName;
      //     }
      //}

      // public static string CurrentUserID
      // {
      //     get
      //     {
      //         string userID = "";
      //         if (HttpContext.Current.User.Identity.IsAuthenticated)
      //             userID = webapp4U.BOL.User.GetUserIDByUserName(CurrentUserName);
      //         return userID;
      //     }
      // }

      
       public static string CurrentUrl
       {
           get
           {
               string strUrl = HttpContext.Current.Request.Url.ToString().Trim();
               return strUrl;
           }
       }

       
      public static bool LangVietNam()
      {
           if (HttpContext.Current.Session["lang"].ToString() == "vi-vn" || HttpContext.Current.Session["lang"].ToString() == "" || HttpContext.Current.Session["lang"] == null)
               return true;
           else
               return false;
      }

      public static string GetLang(string strVietNam, string strEnglish)
      {
           if (LangVietNam() == true)
               return strVietNam;
           else
               return strEnglish;
      }

      //thu muc chua cac Album hinh
      public static String GetGalleryPath()
      {
          try
          {
              return PhysicalApplicationPath() + "//Gallery//";
          }
          catch { return ""; }
      }


      public static string ImageTip()
      {
          try
          {
              return ApplicationPath + "ImageTip/";
          }
          catch { return ""; }
      }
      public static string ImagesUrl()
      {
          try
          {
              return ApplicationPath + "Image/";
          }
          catch { return ""; }
      }

      public static String SaveCapcharImagesUrlPath()
      {
          try
          {
              return PhysicalApplicationPath() + "//Image//";
          }
          catch { return ""; }
      }
      //khoi
      public static string LogUrl()
      {
          try
          {
              return ApplicationPath + "LogError/";
          }
          catch { return ""; }
      }
      //khoi
      public static string LoginLog()
      {
          try
          {
              return ApplicationPath + "LogLogIn/";
          }
          catch { return ""; }
      }
      //khoi
      public static string CssPath()
      {
          try
          {
              return ApplicationPath + "Css/";
          }
          catch { return ""; }
      }
      //khoi
      public static string IconButtonPath()
      {
          try
          {
              return ApplicationPath + "IconButton/";
          }
          catch { return ""; }
      }
       //khoi
      public static string ScriptPath()
      {
          try
          {
              return ApplicationPath + "Scripts/";
          }
          catch { return ""; }
      }
      //khoi
      public static string LayoutPath()
      {
          try
          {
              return ApplicationPath + "Layout/";
          }
          catch { return ""; }
      }
      public static string ImagesUrlAdmin()
       {
           try
           {
               return ApplicationPath + "Admin/Images/";
           }
           catch { return ""; }
       }


       public static string ImagesUrlWebMaster()
       {
           try
           {
               return ApplicationPath + "Icon/";
           }
           catch { return ""; }
       }

       
           
      public static string FullApplicationPath
      {
          get
          {
              return HttpContext.Current.Request.Url.AbsoluteUri.Replace(
                 HttpContext.Current.Request.Url.PathAndQuery, "") + ApplicationPath;
          }
      }

       public static string ApplicationPath
       {
           get
           {
               string url = HttpContext.Current.Request.ApplicationPath;
               if (url.EndsWith("/"))
                   return url;
               else
                   return url + "/";
           }
       }


       public static string PhysicalApplicationPath()
       {
           return HttpContext.Current.Request.PhysicalApplicationPath.ToString();
       }
       

       public static int GetIntFromQueryString(string key)
       {
           int returnValue = -1;
           string queryStringValue;

           // Attempt to get the value from the query string
           //
           queryStringValue = HttpContext.Current.Request.QueryString[key];

           // If we didn't find anything, just return
           //
           if (queryStringValue == null)
               return returnValue;

           // Found a value, attempt to conver to integer
           //
           try
           {

               // Special case if we find a # in the value
               //
               if (queryStringValue.IndexOf("#") > 0)
                   queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

               returnValue = Convert.ToInt32(queryStringValue);
           }
           catch { }

           return returnValue;

       }


       public static string GetStringFromQueryString(string key)
       {
           string returnValue = "";
           string queryStringValue;

           // Attempt to get the value from the query string
           //
           queryStringValue = HttpContext.Current.Request.QueryString[key];

           // If we didn't find anything, just return
           //
           if (queryStringValue == null)
               return returnValue;

           // Found a value, attempt to conver to integer
           //
           try
           {

               // Special case if we find a # in the value
               //
               if (queryStringValue.IndexOf("#") > 0)
                   queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

               returnValue = Convert.ToString(queryStringValue);
           }
           catch { }

           return returnValue;
       }


       public static Int32 ConvertToInt32(object obj)
       {
           try
           {
               return Convert.ToInt32(obj);
           }
           catch { return -1; }
       }


       public static bool IsDate(string Text)
       {
           bool result = true;
           DateTime date;

           try
           {
               date = Convert.ToDateTime(Text);
           }
           catch
           {
               result = false;
           }

           return result;
       }


       //public static string GetButtonAddToCart(int ProductID)
       //{
       //    webapp4U.BOL.Products obj = webapp4U.BOL.Products.GetProductsByID(ProductID);
       //    if (obj.UnitPrice > 0)
       //    {
       //        string strbutton = "";
       //        strbutton = strbutton + "<table cellpadding=\"0\" cellspacing=\"0\">";
       //        strbutton = strbutton + "    <tr>";
       //        strbutton = strbutton + "        <td><img src=" + Globals.ImagesUrl() + "button_left.gif /></td>";
       //        strbutton = strbutton + "        <td background=" + Globals.ImagesUrl() + "button_bg.gif height=\"20px\" align=\"center\">";
       //        strbutton = strbutton + "            <a class=\"Addtocart\" href=" + Globals.ApplicationPath + "Products/ShoppingCart.aspx?ProductID=" + ProductID + "&Action=AddToCart>";
       //        strbutton = strbutton + Globals.GetLang("Thêm vào giỏ hàng", "Add to cart");
       //        strbutton = strbutton + "            </a>";
       //        strbutton = strbutton + "        </td>    ";
       //        strbutton = strbutton + "<td><img src=" + Globals.ImagesUrl() + "button_right.gif /></td>";
       //        strbutton = strbutton + "    </tr>";
       //        strbutton = strbutton + "</table>";
       //        return strbutton;
       //    }
       //    return "";
       //}
      

   }
}